/**
 * @projectName s-pay-mall-21675-zj
 * @package com.zhang.service.impl
 * @className com.zhang.service.impl.LoginService
 * @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
 */
package com.zhang.service.impl;

import com.google.common.cache.Cache;
import com.zhang.domain.req.WeixinQrCodeReq;
import com.zhang.domain.res.WeixinQrCodeRes;
import com.zhang.domain.res.WeixinTokenRes;
import com.zhang.domain.vo.WeixinTemplateMessageVO;
import com.zhang.service.ILoginService;
import com.zhang.service.weixin.IWeixinApiService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import retrofit2.Call;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * LoginService
 * @description 登录的service
 * @author zhangjun
 * @date 2025/8/17 下午4:41
 * @version 1.0
 */
@Service
public class LoginService implements ILoginService {
    @Value("${weixin.config.app-id}")
    private String appId;
    @Value("${weixin.config.app-secret}")
    private String appSecret;
    @Value("${weixin.config.template_id}")
    private String template_id;
    @Resource
    private IWeixinApiService weixinApiService;
    @Resource
    private Cache<String, String> weixinAccessToken;

    @Resource
    private Cache<String, String> openidToken;

    @Override
    public String createQrCodeTicket() throws IOException {
        // 1.获取微信的api的token
        String token = getAccessToken();
        // 2.获取凭证
        // 2. 生成 ticket
        WeixinQrCodeReq weixinQrCodeReq = WeixinQrCodeReq.builder()
                .expire_seconds(2592000)
                .action_name(WeixinQrCodeReq.ActionNameTypeVO.QR_SCENE.getCode())
                .action_info(WeixinQrCodeReq.ActionInfo.builder()
                        .scene(WeixinQrCodeReq.ActionInfo.Scene.builder()
                                .scene_id(100601)
                                .build())
                        .build())
                .build();
        Call<WeixinQrCodeRes> call = weixinApiService.createQrCode(token, weixinQrCodeReq);
        WeixinQrCodeRes weixinQrCodeRes = call.execute().body();
        assert weixinQrCodeRes != null;
        return weixinQrCodeRes.getTicket();
    }

    private @NotNull String getAccessToken() throws IOException {
        String token = weixinAccessToken.getIfPresent(appId);
        if (token == null) {
            Call<WeixinTokenRes> clientCredential = weixinApiService.getToken("client_credential", appId, appSecret);
            WeixinTokenRes body = clientCredential.execute().body();
            assert weixinAccessToken != null;
            token = body.getAccess_token();
            weixinAccessToken.put(appId, token);
        }
        return token;
    }

    @Override
    public String checkLogin(String ticket) {
        return openidToken.getIfPresent(ticket);
    }

    @Override
    public void saveLoginState(String ticket, String openId) throws IOException {
        // 1.保存登录态
        openidToken.put(ticket, openId);
        // 2.获取token
        String token = getAccessToken();
        // 3.发送模板消息
        // 2. 发送模板消息
        Map<String, Map<String, String>> data = new HashMap<>();
        WeixinTemplateMessageVO.put(data, WeixinTemplateMessageVO.TemplateKey.USER, openId);

        WeixinTemplateMessageVO templateMessageDTO = new WeixinTemplateMessageVO(openId, template_id);
        templateMessageDTO.setUrl("https://baidu.com");
        templateMessageDTO.setData(data);

        Call<Void> call = weixinApiService.sendMessage(token, templateMessageDTO);
        call.execute();
    }


}
 